package com.example.administrator.biyesheji_print.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.biyesheji_print.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2018/4/2.
 */

public class mDocumenAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<HashMap<String,Object>> strings;
    private Context context;
    private View.OnClickListener onAdd;
    private View.OnClickListener onSub;
    public OnRecyclerViewLongItemClickListener mOnLongItemClickListener = null;//长按
    public interface OnRecyclerViewLongItemClickListener{
        void onItemLongClick(View view,int position);
    }
    public void setOnAdd(View.OnClickListener onAdd){
        this.onAdd=onAdd;
    }
    public void setOnSub(View.OnClickListener onSub){
        this.onSub=onSub;
    }
    public mDocumenAdapter(ArrayList<HashMap<String,Object>> strings, Context context) {
        this.context=context;
        this.strings=strings;
    }
    public void setOnLongItemClickListener(OnRecyclerViewLongItemClickListener listener) {
        this.mOnLongItemClickListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.document_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    public void remove(int position){
        strings.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
//        Log.i("text",strings.size()+"strings");
            //Log.i("text",strings.get(position).get("filename").toString());
            ((ViewHolder)holder).tv_filename.setText(strings.get(position).get("filename").toString());
        ((ViewHolder)holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int position = holder.getLayoutPosition(); // 1
                mOnLongItemClickListener.onItemLongClick(holder.itemView,position); // 2
                return true;//消耗了事件
            }
        });
           // Log.i("text",strings.get(position).get("type").toString()+"type");
        ((ViewHolder)holder).tv_printtype.setText(strings.get(position).get("type").toString());
        ((ViewHolder)holder).tv_count.setText(strings.get(position).get("count").toString());
        ((ViewHolder)holder).bt_add.setOnClickListener(onAdd);
        ((ViewHolder)holder).bt_min.setOnClickListener(onSub);
        ((ViewHolder)holder).bt_add.setTag(position);
        ((ViewHolder)holder).bt_min.setTag(position);
            if (strings.get(position).get("filename").toString().contains("txt")){
                ((ViewHolder)holder).iv_file.setImageResource(R.drawable.file);
            }
        if (strings.get(position).get("filename").toString().contains("doc")){
            ((ViewHolder)holder).iv_file.setImageResource(R.drawable.doc);
        }
        if (strings.get(position).get("filename").toString().contains("png")){
            ((ViewHolder)holder).iv_file.setImageResource(R.drawable.png);
        }
        if (strings.get(position).get("filename").toString().contains("docx")){
            ((ViewHolder)holder).iv_file.setImageResource(R.drawable.docx);
        }
        if (strings.get(position).get("filename").toString().contains("pdf")){
            ((ViewHolder)holder).iv_file.setImageResource(R.drawable.pdf);
        }
       // ((ViewHolder)holder).tv_count.setText("112321");
    }

    @Override
    public int getItemCount() {
        return strings==null?0:strings.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_filename;
        TextView tv_count;
        ImageView iv_file;
        Button bt_add;
        Button bt_min;
        TextView tv_printtype;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_filename=itemView.findViewById(R.id.tv_filename);
            tv_count=itemView.findViewById(R.id.tv_count);
            bt_add=itemView.findViewById(R.id.bt_add);
            bt_min=itemView.findViewById(R.id.bt_min);
            iv_file = itemView.findViewById(R.id.iv_file);
            tv_printtype=itemView.findViewById(R.id.tv_printtype);
        }
    }
}
